package com.running.base.run.config;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
    }
	
	
	// 字符串过滤
    @Bean
    FilterRegistrationBean configCharsetFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new CharacterEncodingFilter());
        registrationBean.setName("encoding-filter");
        Map<String,String> filterParams = new HashMap<>();
        filterParams.put("forceEncoding","true");
        filterParams.put("encoding","UTF-8");
        registrationBean.setInitParameters(filterParams);
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }
    
    // 消息转换器
    @Bean
    StringHttpMessageConverter stringHttpMessageConverter(){
       return new StringHttpMessageConverter(Charset.forName("utf-8"));
    }

    // 消息转换器
    @Bean
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        return new MappingJackson2HttpMessageConverter();
    }


    /**
     * swagger启用
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/", "/static", "/public");
    }
}
